package cn.iocoder.yudao.module.call.controller.admin.ivrrobottimbre;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.ivrrobottimbre.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrrobottimbre.IvrRobotTimbreDO;
import cn.iocoder.yudao.module.call.service.ivrrobottimbre.IvrRobotTimbreService;

@Tag(name = "管理后台 - 机器人音色")
@RestController
@RequestMapping("/call/ivr-robot-timbre")
@Validated
public class IvrRobotTimbreController {

    @Resource
    private IvrRobotTimbreService ivrRobotTimbreService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人音色")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createIvrRobotTimbre(@Valid @RequestBody IvrRobotTimbreSaveReqVO createReqVO) {
        return success(ivrRobotTimbreService.createIvrRobotTimbre(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人音色")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:update')")
    public CommonResult<Boolean> updateIvrRobotTimbre(@Valid @RequestBody IvrRobotTimbreSaveReqVO updateReqVO) {
        ivrRobotTimbreService.updateIvrRobotTimbre(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人音色")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:delete')")
    public CommonResult<Boolean> deleteIvrRobotTimbre(@RequestParam("id") Long id) {
        ivrRobotTimbreService.deleteIvrRobotTimbre(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人音色")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<IvrRobotTimbreRespVO> getIvrRobotTimbre(@RequestParam("id") Long id) {
        IvrRobotTimbreDO ivrRobotTimbre = ivrRobotTimbreService.getIvrRobotTimbre(id);
        return success(BeanUtils.toBean(ivrRobotTimbre, IvrRobotTimbreRespVO.class));
    }

    @GetMapping("/getByFlowId")
    @Operation(summary = "获得机器人音色")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<IvrRobotTimbreRespVO> getByFlowId(@RequestParam("flowId") Long flowId) {
        IvrRobotTimbreDO ivrRobotTimbre = ivrRobotTimbreService.getIvrRobotTimbreByFlowId(flowId);
        return success(BeanUtils.toBean(ivrRobotTimbre, IvrRobotTimbreRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人音色分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<PageResult<IvrRobotTimbreRespVO>> getIvrRobotTimbrePage(@Valid IvrRobotTimbrePageReqVO pageReqVO) {
        PageResult<IvrRobotTimbreDO> pageResult = ivrRobotTimbreService.getIvrRobotTimbrePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrRobotTimbreRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人音色 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrRobotTimbreExcel(@Valid IvrRobotTimbrePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrRobotTimbreDO> list = ivrRobotTimbreService.getIvrRobotTimbrePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人音色.xls", "数据", IvrRobotTimbreRespVO.class,
                        BeanUtils.toBean(list, IvrRobotTimbreRespVO.class));
    }

}